-- UIRadarReset
-- Create by chendh
-- 雷达重置界面

local UNIVERSAL_FALCON = 11

-- 继承自Layer
UIRadarReset = class("UIRadarReset", function()
    return cc.Layer:create();
end);

function UIRadarReset.create()
    return UIRadarReset.new();
end

--构造函数
function UIRadarReset:ctor()
    self:setName("UIRadarReset");

    -- 初始化/复用飞船说明界面
    local node = cc.CSLoader:createNode("layout/sky_city/RadarReset.csb");
    self:addChild(node);
    self.node = node;
    self.selectIndex = 1;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 重绘
    self:redraw();

    self:redrawItems();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 适配
    self:resize();
end

-- 重绘
function UIRadarReset:redraw()
    local exploreLevel = ME.user.dbase:query("explore_level", 0);
    local resetInfo = SkyExploreM.queryReset(self.selectIndex);

    -- 标题
    local titleLabel = findChildByName(self.node,"CT/title_text");
    local descLabel  = findChildByName(self.node,"CT/desc");
    local tipLabel = findChildByName(self.node,"CT/tip");

    -- 标题用艺术字体
    TextStyleM.setTextStyle(descLabel,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    TextStyleM.setTextStyle(tipLabel,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    titleLabel:setString(getLocStr("reset_radar_title"));
    TextStyleM.setArtWordTitleStyle(titleLabel);

    -- 描述和提示
    applyString(descLabel, string.format(getLocStr("reset_radar_desc"), exploreLevel));

    local coolTime = SkyExploreM.leftResetTime();

    if coolTime > 0 then
        local timeDesc;
        if coolTime > 3600 then
            timeDesc = math.floor(coolTime / 3600) .. "h";
        elseif coolTime > 60 then
            timeDesc = math.floor(coolTime / 60) .. "m";
        else
            timeDesc = math.floor(coolTime) .. "s";
        end

        applyString(tipLabel, string.format(getLocStr("reset_radar_tip3"), timeDesc));
    else
        if exploreLevel >= resetInfo["level_request"] then
            applyString(tipLabel, string.format(getLocStr("reset_radar_tip1"), resetInfo["level"], resetInfo["level_request"]));
        else
            applyString(tipLabel, string.format(getLocStr("reset_radar_tip2"), resetInfo["level"], resetInfo["level_request"]));
        end
    end

    -- 按钮
    local cost = resetInfo["cost"];

    local btnOK = findChildByName(self.node, "CT/btn_ok");
    local iconImg = btnOK:getChildByName("icon");
    local costLabel = btnOK:getChildByName("cost");
    local costValueLabel = btnOK:getChildByName("cost_value");

    TextStyleM.setTextStyle(costLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, nil, nil, nil, nil, nil, true);

    TextStyleM.setTextStyle(costValueLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, nil, nil, nil, nil, nil, true);

    costLabel:setString(getLocStr("btn_text_cost"));

    if cost["money"] then
        costValueLabel:setString(cost["money"]);
        iconImg:loadTexture(getSmallAttribIconPath("money"));
    else
        costValueLabel:setString(cost["gem"]);
        iconImg:loadTexture(getSmallAttribIconPath("gem"));
    end

    -- 选中的选项可以重置，则按钮可点击否则灰化
    if SkyExploreM.canResetExplore(self.selectIndex) then
        btnOK:setBright(true);
        btnOK:setTouchEnabled(true);
    else
        btnOK:setBright(false);
        btnOK:setTouchEnabled(false);
    end
end

-- 绘制选项
function UIRadarReset:redrawItems()
    local coolTime = SkyExploreM.leftResetTime();
    local exploreLevel = ME.user.dbase:query("explore_level", 0);

    for index = 1, 6 do
        local resetInfo = SkyExploreM.queryReset(index);

        local item = findChildByName(self.node, "CT/item" .. index);
        local focusImg = findChildByName(item, "focus");
        local levelLabel = findChildByName(item, "level");

        item.index = index;

        -- 显示重置后的探索等级
        TextStyleM.setTextStyle(levelLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_WHITE, false);
        if resetInfo["level"] <= 0 then
            levelLabel:setString(getLevelStr(0));
        else
            levelLabel:setString(resetInfo["level"]);
        end

        -- 如果是当前选中的选项，则显示焦点图标
        if item.index == self.selectIndex then
            focusImg:setVisible(true);
        else
            focusImg:setVisible(false);
        end

        -- 条件不满足的半透显示
        if SkyExploreM.canResetExplore(item.index) then
            item:setOpacity(255);
        else
            item:setOpacity(150);
        end
    end
end

-- 注册点击事件
function UIRadarReset:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("ship_return");
            UIMgr.getCurrentScene():removeFormByName("UIRadarReset");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 重置
            -- 如果当前有宇宙隼的光环需要提醒玩家根据玩家选择
            self:tryResetRadar();
        end
    end
    btnOk:addTouchEventListener(onOkClick);

    local function onItemClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            local item = sender:getParent();
            local lastIndex = self.selectIndex;

            self.selectIndex = item.index;

            local lastItem = findChildByName(self.node, "CT/item" .. lastIndex);
            local focusImg = lastItem:getChildByName("focus");
            focusImg:setVisible(false);

            local curFocusImg = item:getChildByName("focus");
            curFocusImg:setVisible(true);

            -- 重绘下
            self:redraw();
        end
    end


    for index = 1, 6 do
        local item = findChildByName(self.node, "CT/item" .. index);
        local bgImg = findChildByName(item, "bg");

        bgImg:addTouchEventListener(onItemClick);
    end
end

-- 适配
function UIRadarReset:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 尝试重置雷达
function UIRadarReset:tryResetRadar()
    -- 检查下宇宙隼的光环
    if NewAirShipM.hasOwnShip(UNIVERSAL_FALCON) then
        local resetInfo = SkyExploreM.queryReset(self.selectIndex);
        local desc = getLocStr("active_universe_falcon");
        local addon = 0;
        local prop = PropM.combine(ME.user, "reset_level", 1);
        addon = PropM.apply(prop, addon);

        local resetLevel = resetInfo["level"];
        desc = string.format(desc, resetLevel + addon);

        local title = getLocStr("reset_radar_title");

        local startUsingText = getLocStr("btn_text_start_using");
        local directResetText = getLocStr("btn_text_direct_reset");

        local function startUsing()
            SkyExploreM.resetExplore(self.selectIndex);
            UIMgr.getCurrentScene():removeFormByName("UIRadarReset");
        end

        -- 选择直接重置
        local function directReset()
            SkyExploreM.resetExplore(self.selectIndex, 1);
            UIMgr.getCurrentScene():removeFormByName("UIRadarReset");
        end

        confirm2(CONFIRM_TYPE_OK_CANCEL, title, desc, startUsing, directReset, startUsingText, directResetText, {["cancelColor"] = "blue"});
    else
        -- 直接重置
        SkyExploreM.resetExplore(self.selectIndex);

        UIMgr.getCurrentScene():removeFormByName("UIRadarReset");
    end
end